(function($){
"use strict";
$.fn.layout=function(options){
	options = $.extend(true,{}, options||{});
    var handler=(function(){
        var handler=function(box,options){
			return new handler.prototype.init(box, options);
		};
        handler.prototype={
            init:function(box, options){
				var that = this;
				$(['Height', 'Width']).each(function(i, one){
					that['getView'+one] = (function () {
						var container = "BackCompat" === document.compatMode ? document.body : document.documentElement;
						return function () {
							return container['client'+one];
						};
					}());
				});
				var getPageHeight = (function () {
					var body = document.body;
					var html = document.documentElement;
					var container = "BackCompat" === document.compatMode ? body : html;
					return function () {
						return Math.max(html.scrollHeight, body.scrollHeight, container.clientHeight);
					};
				}());
				this.box = box.get(0);
				this.userOptions = options;
				var quirks = document.documentMode==5;
				var bar_style = options.borderStyle;
				$(box).addClass('layout-container').css({position:'relative'});
				this.panels = {
					north: $('>.layout-north', box).css({
						width:'100%',
						'border-bottom':bar_style,
						position:'absolute',
						'top':0,
						'left':0
					}).get(0),
					south: $('>.layout-south', box).css({
						width:'100%',
						'border-top':bar_style,
						position:'absolute',
						'bottom':0,
						'left':0
					}).get(0),
					west: $('>.layout-west', box).css({
						border:bar_style,
						'border-left':'none',
						position:'absolute',
						'left':0
					}).get(0),
					east: $('>.layout-east', box).css({
						border:bar_style,
						'border-right':'none',
						position:'absolute',
						'right':0
					}).get(0),
					center: $('>.layout-center', box).css({
						border:bar_style,
						position:'absolute'
					}).get(0)
				};
				this.resize();
				this.appendResizeBars();
            },
			getNum: function(s){
				return parseInt(s)||0;
			},
			appendResizeBars: function(){
				var bars = {};
				var panels = this.panels;
				var barWidth = this.userOptions.barWidth;
				var box = this.box;
				if(panels.north){
					bars.north = $('<div class="resize-bar b-north" style="width:100%;cursor:n-resize;"><span></span></div>').appendTo(box).css({height:barWidth, top:panels.north.offsetHeight});
				}
				if(panels.south){
					bars.south = $('<div class="resize-bar b-south" style="width:100%;cursor:n-resize;"><span></span></div>').appendTo(box).css({height:barWidth, bottom:panels.south.offsetHeight});
				}
				if(panels.west){
					bars.west = $('<div class="resize-bar b-west" style="cursor:e-resize;"><span></span></div>').appendTo(box).css({width:barWidth, top:panels.north.offsetHeight+barWidth, left:panels.west.offsetWidth, height:panels.west.offsetHeight});
				}
				if(panels.east){
					bars.east = $('<div class="resize-bar b-east" style="cursor:e-resize;"><span></span></div>').appendTo(box).css({width:barWidth, top:panels.north.offsetHeight+barWidth, right:panels.east.offsetWidth, height:panels.east.offsetHeight});
				}
				this.resizeBars = bars;
			},
			resize: function(){
				var barWidth = this.userOptions.barWidth;
				var ie6 = /MSIE 6/.test(navigator.userAgent);
				var ie7 = (function(){
					var dm = document.documentMode;
					return /MSIE 7/.test(navigator.userAgent)&&dm!==8&&dm!==5 || dm==7;
				})();
				var box = this.box,
					panels = this.panels,
					getViewWidth = this.getViewWidth,
					getViewHeight = this.getViewHeight,
					getNum = this.getNum;
				$(box).css({height:getViewHeight()});
				if(panels.north){
					$(panels.north).css({
						'height': panels.north.offsetHeight - (ie6||ie7?getNum(panels.north.style.borderTopWidth)*2+getNum(panels.north.style.borderBottomWidth)*2:0)
					});
				}
				if(panels.south){
					$(panels.south).css({
						'height': panels.south.offsetHeight - (ie6||ie7?getNum(panels.south.style.borderTopWidth)*2+getNum(panels.south.style.borderBottomWidth)*2:0)
					});
				}
				var mh = box.offsetHeight - (panels.north ? panels.north.offsetHeight + barWidth : 0) - (panels.south ? panels.south.offsetHeight + barWidth : 0);
				if(panels.west){
					$(panels.west).css({
						'top': (panels.north ? panels.north.offsetHeight + barWidth : 0),
						'width': panels.west.offsetWidth - (ie6||ie7?getNum(panels.west.style.borderLeftWidth)*2+getNum(panels.west.style.borderRightWidth)*2:0),
						'height': mh - (ie6||ie7?(getNum(panels.west.style.borderTopWidth)+getNum(panels.west.style.borderBottomWidth)):0)
					});
				}
				if(panels.east){
					$(panels.east).css({
						'top': (panels.north ? panels.north.offsetHeight + barWidth : 0),
						'width': panels.east.offsetWidth - (ie6||ie7?getNum(panels.east.style.borderLeftWidth)*2+getNum(panels.east.style.borderRightWidth)*2:0),
						'height': mh - (ie6||ie7?getNum(panels.east.style.borderTopWidth||0)+getNum(panels.east.style.borderBottomWidth||0):0)
					});
				}
				var mw = box.offsetWidth - (panels.west ? panels.west.offsetWidth + barWidth : 0) - (panels.east ? panels.east.offsetWidth + barWidth : 0);
				if(panels.center){
					var sc = panels.center.style;
					$(panels.center).css({
						'top': (panels.north ? panels.north.offsetHeight + barWidth : 0),
						'left': (panels.west ? panels.west.offsetWidth + barWidth : 0),
						'width': mw -(ie6||ie7?getNum(sc.borderLeftWidth)+getNum(sc.borderRightWidth):0),
						'height': mh - (ie6||ie7?getNum(sc.borderTopWidth)+getNum(sc.borderBottomWidth):0)
					});
				}
				// $(panels.north).html("H:"+panels.north.offsetHeight+", W:"+panels.north.offsetWidth);
				// $(panels.south).html("H:"+panels.south.offsetHeight+", W:"+panels.south.offsetWidth);
				// $(panels.east).html("H:"+panels.east.offsetHeight+", W:"+panels.east.offsetWidth);
				// $(panels.west).html("H:"+panels.west.offsetHeight+", W:"+panels.west.offsetWidth);
				// $(panels.center).html("H:"+panels.center.offsetHeight+", W:"+panels.center.offsetWidth);
			}
        };
        handler.prototype.init.prototype = handler.prototype;
        return handler;
    })();
 
	// return this.each(function(){ handler(this, options); });
	return handler(this, options);
};
})(jQuery);
